<?php


namespace app\backend\controller\v1;
use app\backend\validate\StudentValidate;
use think\exception\ValidateException;
use app\common\model\Student as StudentModel;
use think\facade\Filesystem;

class Student extends BaseController
{
    /**
     *个人信息列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // where条件
        $where = input('name');
        //获取列表
        $list=StudentModel::with('major')
            ->with('school')
            //排序
            ->order('id', 'asc')
            ->field('*')
            ->where(function ($query) use ($where) {
                $query->where('name', 'like', '%' . $where . '%');
            })
            ->paginate(10);

        foreach ($list as &$v){
            $v['image'] = config("app.domain_url") .$v['image'];
            $v['major_id'] = $v->major->name;
            $v['school_id'] = $v->school->name;
            //销毁变量
            unset($v->major);
            unset($v->school);
        }
        //分页
        $page = $list->render();
        return sucessMsg(200, '获取成功！', ['list' => $list, 'total' => count($list),'page'=>$page]);
    }

    /**
     * 显示创建
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = new StudentModel();
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(StudentValidate::class)->scene('create')->check($data);
            if (empty($img)) {
                return errMsg(422, '请上传图片!');
            }
            $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
            $data['image'] = $saveName;
            $data['image'] = str_replace('\\', '/', $data['image']);
            if ($user->save($data)) {
                return sucessMsg(200, '创建成功！', $user->id);
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择学生！');
        }
        $user = StudentModel::find($id);
        if (empty($user)) {
            return errMsg(400, '所选取的数据为空！');
        }
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(StudentValidate::class)->scene('update')->check($data);
            if (!empty($img)) {
                $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
                $data['image'] = $saveName;
                $data['image'] = str_replace('\\', '/', $data['image']);
            }
            if (StudentModel::update($data, ['id'=>$id])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除用户id！');
        }
        $user = StudentModel::find($id);
        if (empty($user)) {
            return errMsg(400, '所选取的数据为空！');
        }
        if (StudentModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}